package com.yuma.container;


import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

public class NotsafeDemo {

    public static void main(String[] args) {

//        List<String> list = new ArrayList<>();
        // 1.解决Vector替换
        // List<String> list = new Vector<>();

        // 2.Collections.synchronizedList
        // List<String> list = Collections.synchronizedList(new ArrayList<>());

        // 3.CopyOnWriteArrayList: 当修改集合的时候拷贝一份去修改，修改后把原集合引用指向新的集合
        List<String> list = new CopyOnWriteArrayList();

        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                list.add(UUID.randomUUID().toString().substring(0, 8));//线程不安全
                System.out.println(list.hashCode());//调用了ArrayList的迭代方法
            }, String.valueOf(i)).start();
        }

        //java.util.ConcurrentModificationException  (同时修改一个不安全的集合异常)
    }
}